package com.ruoyi.web.controller.hatdevice.device;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.device.domain.HatDeviceRole;
import com.ruoyi.device.domain.req.devicebind.DeviceBindReq;
import com.ruoyi.device.domain.resp.devicebind.DeviceBindResp;
import com.ruoyi.device.domain.resp.hatdevice.HatDeviceResp;
import com.ruoyi.device.service.HatDeviceRoleService;
import com.ruoyi.device.service.HatDeviceService;
import com.ruoyi.device.util.UpdateRedisUtils;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
//import org.w3c.dom.stylesheets.LinkStyle;

import java.util.Date;
import java.util.List;

/**
 * @Author wy
 * @Description 设备API
 * @Date 2023/11/24 16:48
 * @Version 1.0
 */
@RestController
@RequestMapping("/device")
public class DeviceController extends BaseController {

    @Autowired
    private HatDeviceService  hatDeviceService;
    @Autowired
    private HatDeviceRoleService hatDeviceRoleService;
    @Autowired
    private UpdateRedisUtils updateRedisUtils;

    /**
     *查询重庆区域所有的设备
     * @return
     */
    @PreAuthorize("@ss.hasPermi('device:map:list')")
    @GetMapping(value = "/data")
    public R<List<HatDeviceResp>> searchAllDevice(){
        List<HatDeviceResp> deviceResps = this.hatDeviceService.searchByOrgCode(Constants.CQ_CODE);
        return R.ok(deviceResps);
    }
    @PostMapping(value = "/searchDeviceBind")
    public R<PageInfo<DeviceBindResp>> searchDeviceBind(@RequestBody DeviceBindReq deviceBindReq){
        PageInfo<DeviceBindResp> test=this.hatDeviceRoleService.searchByOrgCode(deviceBindReq);
        return R.ok(test);
    }
    /**
     * 绑定设备
     * @param hatDeviceRole
     * @return
     */
    @PostMapping("/bind")
    public R  binddevice(@RequestBody HatDeviceRole hatDeviceRole){
        // 新增流程
        hatDeviceRole.setStatus("0");
        hatDeviceRole.setCreateTime(new Date());
        boolean save = this.hatDeviceRoleService.save(hatDeviceRole);
        //更新已经绑定的设备
        this.updateRedisUtils.updateBingDevice();
        if(save){
            return R.ok();
        }else {
            return R.fail("绑定失败");
        }
    }
    /**
     * 解绑设备
     * @param id
     * @return
     */
    @GetMapping("/unbind/{id}")
    public R unBind(@PathVariable("id") Integer id){
        boolean unBinding = this.hatDeviceRoleService.unBinding(id);
        //更新已经绑定的设备
        this.updateRedisUtils.updateBingDevice();
       if(unBinding){
          return R.ok();
       }else {
          return R.fail("绑定失败");
       }
    }

    /**
     * 查询没绑定的设备
     * @return
     */
    @GetMapping("/findunbind")
    public R<List<HatDeviceResp>> find(){
        List<HatDeviceResp> hatDeviceResps = this.hatDeviceService.searchUnBind();
        return R.ok(hatDeviceResps);
    }

}
